// 搜索栏
import 'package:check_app/widgets/PhotpGalleryPage.dart';
import 'package:flutter/material.dart';

class Picture extends StatefulWidget {
  final int count;
  final bool uploadButton;
  final List<String> picList;
  final Function() onAdd;
  final Function(String url) onRemove;
  const Picture({
    Key key,
    this.count,
    this.uploadButton,
    this.picList,
    this.onAdd,
    this.onRemove,
  }) : super(key: key);

  @override
  _PictureState createState() => _PictureState();
}

class _PictureState extends State<Picture> {
  bool uploadButton;
  //记录选择的照片
  List<String> imageList;

  @override
  void initState() {
    super.initState();
    imageList = widget.picList ?? [];
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: GridView.count(
        physics: new NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        //水平子Widget之间间距
        crossAxisSpacing: 10.0,
        //垂直子Widget之间间距
        mainAxisSpacing: 10.0,
        //GridView内边距
        // padding: const EdgeInsets.fromLTRB(0, 15, 0, 15),
        //一行的Widget数量
        crossAxisCount: widget.count ?? '3',
        //子Widget宽高比例
        childAspectRatio: 1,
        children: showPicList(),
      ),
    );
  }

  showPicList() {
    final list = widget.picList ?? [];
    final newList = list.length <= 9 ? list.sublist(0) : list.sublist(0, 9);
    if (widget.uploadButton == null || widget.uploadButton == true) {
      if (newList.length == 0 ||
          (newList[newList.length - 1] != 'add' && newList.length < 9)) {
        newList.add('add');
      }
      imageList = newList.sublist(0, newList.length - 1);
    } else {
      imageList = newList;
    }

    return getList(newList);
  }

  List<Widget> getList(List list) {
    return list
        .asMap()
        .keys
        .map<Widget>((index) => createItem(list[index], index))
        .toList();
  }

  Widget createItem(item, index) {
    if (item != 'add') {
      return GestureDetector(
        onTap: () async {
          await Navigator.of(context).push(MaterialPageRoute(builder: (_) {
            return PhotpGalleryPage(
              index: index,
              photoList: imageList,
            );
          }));
        },
        onLongPress: () {
          widget.onRemove(item);
        },
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(
              width: 1,
              color: Color.fromRGBO(229, 229, 229, 1),
            ),
            borderRadius: BorderRadius.circular(2),
          ),
          child: Image.network(item),
        ),
      );
    } else {
      return GestureDetector(
        onTap: () {
          widget.onAdd();
        },
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            border: Border.all(
              width: 1,
              color: Color.fromRGBO(229, 229, 229, 1),
            ),
            borderRadius: BorderRadius.circular(2),
          ),
          child: Column(
            children: <Widget>[
              Expanded(
                child: Image(
                  image: AssetImage('assets/images/camera.png'),
                  width: 29,
                  height: 25,
                ),
              ),
            ],
          ),
        ),
      );
    }
  }
}
